package com.atguigu.process.service.impl;

import com.atguigu.model.process.ProcessTemplate;
import com.atguigu.process.mapper.OaProcessTemplateMapper;
import com.atguigu.process.service.OaProcessService;
import com.atguigu.process.service.OaProcessTemplateService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 审批模板 服务实现类
 * </p>
 *
 * @author 赵星
 * @since 2023-12-21
 */
@Service
public class OaProcessTemplateServiceImpl extends ServiceImpl<OaProcessTemplateMapper, ProcessTemplate> implements OaProcessTemplateService {

    @Autowired
    private OaProcessTemplateMapper processTemplateMapper;
    @Autowired
    private OaProcessService processService;

    @Override
    public Page<ProcessTemplate> selectPage(Page<ProcessTemplate> pageParam) {
        //分页查询审批模板
        List<ProcessTemplate> processTemplateList = processTemplateMapper.selectPagec((pageParam.getCurrent()-1) * pageParam.getSize(), pageParam.getSize());

        Page<ProcessTemplate> processTemplatePage = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        processTemplatePage.setRecords(processTemplateList);

        //查询所有的审批模板的数量
        LambdaQueryWrapper<ProcessTemplate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProcessTemplate::getIsDeleted, 0);

        Integer count = processTemplateMapper.selectCount(wrapper);
        processTemplatePage.setTotal(count);

        return processTemplatePage;
    }

    //发布审批模板
    @Override
    @Transactional
    public void publish(Long id) {
        ProcessTemplate processTemplate = this.getById(id);
        processTemplate.setStatus(1);//1代表发布
        processTemplateMapper.updateById(processTemplate);

        //部署流程定义
        if (!StringUtils.isEmpty(processTemplate.getProcessDefinitionPath())) {
            processService.deployZip(processTemplate.getProcessDefinitionPath());
        }
    }
}
